var theLog;
var menu;

function Menu(menuId) {
	var me = this;
	this.menuId = menuId;

	// Get panels from menu data
	var menuItems = Array();
	$("#" + menuId + " li").each(function(i, e) {
		e=$(e);
		menuItems[e.attr("id")] = {
			id: e.attr("id"),
			e: e,
			panelId: e.attr("data-panelId")};
	});
	this.menuItems = menuItems;

	// Get panels from menu data
	var panels = Array();
	for (i in menuItems) {
		var menuItem = menuItems[i];
		var e = $("#" + menuItem.panelId);
		panels[menuItem.panelId] = { id:menuItem.panelId, e:e, menuItem:menuItem };
	}
	this.panels = panels;

	this.showPanel = function(panelId) {
		$(".panel").addClass("hidePanel");	// hide all panels

		var panel = panels[panelId];
		panel.e.removeClass("hidePanel");	// show the correct panel

		var foo = true;
		for (i in menuItems) {
			var item = menuItems[i];
			if (foo)
				item.e.removeClass("disabledMenu");
			else
				item.e.addClass("disabledMenu");

			if (i == panel.menuItem.id) {
				foo = false;
				item.e.addClass("activeMenu");
			}
			else
				item.e.removeClass("activeMenu");
		}
	}

	this.clicked = function(e) {
		// get menu item
		var xx = $(e.target);
		var id = xx.attr("id");
		var menuItem = menuItems[id];
		if (!xx.hasClass("disabledMenu")) {	// if not disabled
			// show panel
			me.showPanel(menuItem.panelId);
		}
	}
}

function go() {
	var logTxt = $("#logTxt").val();
	var logs = parseText(logTxt);

	theLog = logs;

	showPlayerSummary(logs);
}

function showPlayerSummary(playerLogs) {
	menu.showPanel("playerSummaryPanel");

	var summaryData = Array();
	for (name in playerLogs) {
		var row = Array();
		row["Player name"] = name;
		row["dps"] = num2str(4231.1231, 2);

		row.onCreate = createSelectPlayerClickDelegate(name, playerLogs);
		summaryData.push(row);
	}
	renderTable($("#playerSummaryTable"), summaryData);
}

function createSelectPlayerClickDelegate(name, playerLogs) {
	return function(e) { e.click(createSelectPlayerDelegate(name, playerLogs[name])); };
}

function createSelectPlayerDelegate(name, log) {
	return function(e) { showPlayer(name, log); };
}

function num2str(num, decimals) {
	if (decimals == undefined) {
		return Math.round(num);
	}
	else {
		var f = Math.pow(10, decimals);
		return Math.round(num * f) / f;
	}
}

function paddStr(str, padd) {
	return (padd + str).substr(-padd.length);
}

function date2str(date) {
	var year = "" + date.getFullYear();
	var month = paddStr(date.getMonth() + 1, "00");
	var day = paddStr(date.getDate(), "00");
	return year + "-" + month + "-" + day + " " + time2str(date);
}

function time2str(date) {
	var hour = paddStr(date.getHours(), "00");
	var minute = paddStr(date.getMinutes(), "00");
	var second = paddStr(date.getSeconds(), "00");
	return hour + ":" + minute + ":" + second;
}

function showPlayer(name, log) {
	menu.showPanel("playerDetailsPanel");
	var stats = new Stats(log);

	var str = "";
	str += name + "<br />";
	str += "entries: " + stats.entries + "<br />";
	str += "startTime: " + date2str(stats.startTime) + "<br />";
	str += "endTime: " + date2str(stats.endTime) + "<br />";
	str += "duration: " + num2str(stats.duration) + " s<br />";

	str += "totalDamage: " + num2str(stats.totalDamage) + "<br />";
	str += "totalHeal: " + num2str(stats.totalHeal) + "<br />";
	str += "totalDps: " + num2str(stats.totalDps, 2) + "<br />";
	str += "totalHps: " + num2str(stats.totalHps, 2) + "<br />";

	$("#outputLog").html(str);

	var damageTable = Array();
	for (i in stats.spells) {
		var s = stats.spells[i];
		var row = {};
		row["Name"] = i;
		row["dps"] = num2str(s.totalDamage / stats.duration, 2);
		row["Total"] = num2str(s.totalDamage);
		row["Count"] = s.count;
		row["Min"] = num2str(s.min, 0);
		row["Max"] = num2str(s.max, 0);
		row["Average"] = num2str(s.totalDamage / s.count, 2);
		row["%"] = num2str(s.percentage, 1) + "%";
		damageTable.push(row);
	}
	renderTable($("#damageTable"), damageTable);
	var healingTable = Array();
	for (i in stats.healSpells) {
		var s = stats.healSpells[i];
		var row = {};
		row["Name"] = i;
		row["dps"] = num2str(s.totalHealing / stats.duration, 2);
		row["Total"] = num2str(s.totalHealing);
		row["Count"] = s.count;
		row["Min"] = num2str(s.min, 0);
		row["Max"] = num2str(s.max, 0);
		row["Average"] = num2str(s.totalHealing / s.count, 2);
		row["%"] = num2str(s.percentage, 1) + "%";
		healingTable.push(row);
	}
	renderTable($("#healingTable"), healingTable);
}

function renderTable(t, data) {
	t.empty();
	var headerRow = $("<tr></tr>");

	var headers = Array();

	for (caption in data[0]) {
		if (caption != "onCreate") {
			var headerCell = $("<th>" + caption + "</th>");
			headerRow.append(headerCell);
			headers.push(caption);
		}
	}
	t.append(headerRow);

	for (i in data) {
		var dataRow = $("<tr></tr>");
		for (j in headers) {
			var dataCell = $("<td>" + data[i][headers[j]] + "</td>");
			dataRow.append(dataCell);
		}
		if (data[i].onCreate) {
			data[i].onCreate(dataRow);
		}
		t.append(dataRow);
	}
}

/* MENU
*****************************************************/

function selectMenu1() {
	menu.showPanel("inputPanel");
}

function selectMenu2() {
	menu.showPanel("playerSummaryPanel");
}

function selectMenu3() {
	menu.showPanel("playerDetailsPanel");
}

/* Initiation
*****************************************************/

$(document).ready(function() {
	menu = new Menu("StepMenu");
	$("#MenuStep1").click(menu.clicked);
	$("#MenuStep2").click(menu.clicked);
	$("#MenuStep3").click(menu.clicked);
	menu.showPanel("inputPanel");
});